<template>
  <basic-container>
    <avue-crud :option="option"
    :data="data"
    :page="page"
    :permission="permissionList"
    :before-open="beforeOpen"
    v-model="form"
    @row-update="rowUpdate"
    @row-save="rowSave"
    @row-del="rowDel"
    @search-change="searchChange"
    @search-reset="searchReset"
    @selection-change="selectionChange"
    @current-change="currentChange"
    @size-change="sizeChange"
    @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.cloudfile_delete"
          @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/file/cloudfile";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "id",
              prop: "id",
              rules: [{
                required: true,
                message: "请输入id",
                trigger: "blur"
              }]
            },
            {
              label: "企业id",
              prop: "corpid",
              rules: [{
                required: true,
                message: "请输入企业id",
                trigger: "blur"
              }]
            },
            {
              label: "用户id",
              prop: "userid",
              rules: [{
                required: true,
                message: "请输入用户id",
                trigger: "blur"
              }]
            },
            {
              label: "分类id",
              prop: "typeId",
              rules: [{
                required: true,
                message: "请输入分类id",
                trigger: "blur"
              }]
            },
            {
              label: "文件网络地址",
              prop: "fileUrl",
              width: '110',
              rules: [{
                required: true,
                message: "请输入文件网络地址",
                trigger: "blur"
              }]
            },
            {
              label: "文件名称",
              prop: "fileName",
              rules: [{
                required: true,
                message: "请输入文件名称",
                trigger: "blur"
              }]
            },
            {
              label: "文件OSSKEY",
              prop: "fileOsskey",
              width: '110',
              rules: [{
                required: true,
                message: "请输入文件OSSKEY",
                trigger: "blur"
              }]
            },
            {
              label: "文件格式",
              prop: "fileExt",
              rules: [{
                required: true,
                message: "请输入文件格式",
                trigger: "blur"
              }]
            },
            {
              label: "文件大小(k)",
              width: '100',
              prop: "fileSize",
              rules: [{
                required: true,
                message: "请输入文件大小(k)",
                trigger: "blur"
              }]
            },
            {
              label: "创建人",
              prop: "createName",
              rules: [{
                required: true,
                message: "请输入创建人",
                trigger: "blur"
              }]
            },
            {
              label: "创建时间",
              prop: "createTime",
              rules: [{
                required: true,
                message: "请输入创建时间",
                trigger: "blur"
              }]
            },
            {
              label: "编辑人",
              prop: "editName",
              rules: [{
                required: true,
                message: "请输入编辑人",
                trigger: "blur"
              }]
            },
            {
              label: "编辑时间",
              prop: "editTime",
              rules: [{
                required: true,
                message: "请输入编辑时间",
                trigger: "blur"
              }]
            },
            {
              label: "是否共享",
              prop: "isshare",
              rules: [{
                required: true,
                message: "请输入是否共享",
                trigger: "blur"
              }]
            },
            {
              label: "备注",
              prop: "remarks",
              rules: [{
                required: true,
                message: "请输入备注",
                trigger: "blur"
              }]
            },
            {
              label: "状态（-1：删除，0：正常）",
              prop: "state",
              width: '200',
              rules: [{
                required: true,
                message: "请输入状态（-1：删除，0：正常）",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.cloudfile_add, false),
          viewBtn: this.vaildData(this.permission.cloudfile_view, false),
          delBtn: this.vaildData(this.permission.cloudfile_delete, false),
          editBtn: this.vaildData(this.permission.cloudfile_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, loading, done) {
        add(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          done();
          console.log(error);
        });
      },
      rowUpdate(row, index, loading, done) {
        update(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          done();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "success"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "success"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params) {
        this.query = params;
        this.onLoad(this.page, params);
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
        });
      }
    }
  };
</script>

<style>
</style>
